<?php
/**
 * This file is form http://findcat.cn
 *
 * @link     https://findcat.cn
 * @email    1476982312@qq.com
 */
namespace App\Observers;

use App\Models\Category;
use Illuminate\Support\Facades\Cache;

class CategoryObserver
{
    /**
     * 菜单删除事件.
     */
    public function deleted()
    {
        Cache::forget('menu');

        $menu = Category::query()
            ->where('p_id', 0)
            ->where('is_menu', true)
            ->orderBy('sort', 'asc')
            ->get()
            ->pluck('name', 'id');

        Cache::forever('menu', $menu);
    }

    /**
     * 菜单保存或更新事件.
     */
    public function saved()
    {
        Cache::forget('menu');

        $menu = Category::query()
            ->where('p_id', 0)
            ->where('is_menu', true)
            ->orderBy('sort', 'asc')
            ->get()
            ->pluck('name', 'id');

        Cache::forever('menu', $menu);
    }
}
